package itsm.isperp.framework.quartz.provider.impl;

import itsm.isperp.framework.quartz.domain.JobDefinition;
import itsm.isperp.framework.quartz.executor.ScanJobExecutor;
import itsm.isperp.framework.quartz.executor.ScanJobExecutorDetail;
import itsm.isperp.framework.quartz.provider.ISystemJobProvider;
import itsm.isperp.framework.quartz.service.IJobService;

import java.text.ParseException;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Trigger;
import org.quartz.impl.triggers.CronTriggerImpl;

/**
 * 
 * 对系统的定时作用进行扫描的提供者 ，是系统作业提供者{@link ISystemJobProvider}的实现
 * 
 * @author lizx
 * @since 1.0
 * 
 */
public class SystemScanJobProvider implements ISystemJobProvider {

	/**
	 * 定时扫描表达式
	 */
	private String scanJobCronExpression;

	private IJobService<? extends JobDefinition> jobService;

	public JobDetail getJobDetail() {
		ScanJobExecutorDetail<? extends JobDefinition> jobDetail = new ScanJobExecutorDetail<>(
				jobService);
		jobDetail.setKey(new JobKey(ScanJobExecutor.JOB_ID, GROUP_ID));
		jobDetail.setJobClass(ScanJobExecutor.class);
		return jobDetail;
	}

	public Trigger getTrigger() {
		CronTriggerImpl trigger = new CronTriggerImpl();
		trigger.setName("trigger" + ScanJobExecutor.JOB_ID);
		try {
			trigger.setCronExpression(scanJobCronExpression);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return trigger;
	}

	public String getScanJobCronExpression() {
		return scanJobCronExpression;
	}

	public void setScanJobCronExpression(String scanJobCronExpression) {
		this.scanJobCronExpression = scanJobCronExpression;
	}

	public IJobService<? extends JobDefinition> getJobService() {
		return jobService;
	}

	public void setJobService(IJobService<? extends JobDefinition> jobService) {
		this.jobService = jobService;
	}

}
